<?php


namespace models\Business;


use models\DAO\AttributeModel;
use models\Exception\DAO\ModelDriverException;
use models\Exception\DAO\ModelException;
use models\Exception\DAO\ModelReflectionException;
use models\Exception\DAO\ModelSqlException;
use models\Exception\Service\ServiceException;
use models\Exception\Transformer\TransformerException;
use models\Service\AttributeService;

class AlibabaAttribute
{
    /**
     * 将采集来的1688 attribute数据入库
     * @param array $attribute_array
     * @throws ModelDriverException
     * @throws ModelException
     * @throws ModelReflectionException
     * @throws ModelSqlException
     * @throws ServiceException
     * @throws TransformerException
     */
    static public function collectAttribute($attribute_array = []) {
        try {
            // 获取attribute_valu数组
            $attribute_value_arr = array_column($attribute_array, 'attribute_value');
            $attribute_value_arr = array_filter(array_unique($attribute_value_arr));

            // 查询attribute_id是否存在于数据库
            if (count($attribute_value_arr) == 1) {
                $attribute_exists['data'][] = AttributeService::getInstance()->getOne(implode(',', array_values($attribute_value_arr)));
            } else {
                $attribute_exists = AttributeService::getInstance()->getList(1, count($attribute_value_arr), 'desc', 'attribute_id',
                    ['groupOp' => 'AND', 'rules' => [
                        ['field' => 'attribute_value', 'op' => 'in', 'data' => implode(',', $attribute_value_arr)]]]);
            }

            $batch_update_arr = $batch_insert_arr = [];
            // attribute存在
            if (!empty($attribute_exists['data'])) {
                $exists_attribute_value_arr = array_column($attribute_exists['data'], 'attribute_value');
                $exists_attribute_id_arr = array_column($attribute_exists['data'], 'id');
                foreach ($attribute_array as $attribute_info) {
                    // 已存在的商品写入更新数据
                    if (in_array($attribute_info['attribute_value'], $exists_attribute_value_arr)) {
                        $primary_key = array_search($attribute_info['attribute_value'], $exists_attribute_value_arr);
                        if (isset($exists_attribute_id_arr[$primary_key])) {
                            $attribute_info['id'] = $primary_key;
                            array_push($batch_update_arr, $attribute_info);
                        }
                    }
                    // 不存在的商品写入插入数组
                    else
                        array_push($batch_insert_arr, $attribute_info);
                }
            } else {
                $batch_insert_arr = $attribute_array;
            }

            // 更新数组存在
            if (!empty($batch_update_arr)) {
                // 更新数组长度大于1，批量插入
                if (count($batch_update_arr) > 1)
                    AttributeService::getInstance()->batchUpdate($batch_update_arr);
                // 更新数组长度等于1，单条更新
                else {
                    $update_arr = array_values($batch_update_arr);
                    $update_arr = $update_arr[0];
                    $attribute_id = $update_arr['attribute_id'];
                    AttributeService::getInstance()->update($update_arr, $attribute_id);
                }
            }

            // 插入数据存在
            if (!empty($batch_insert_arr)) {
                // 批量插入数据
                AttributeService::getInstance()->batchInsert($batch_insert_arr);
            }

            $sql = "SELECT `id`, `attribute_value` FROM `attribute` WHERE `attribute_value` in ('" . implode("', '", $attribute_value_arr) . "')";
            $res = AttributeModel::getInstance()->getConn()->query($sql)->fetchAll();
            $return_data = [];
            foreach ($res as $row) {
                $return_data[$row['attribute_value']] = $row['id'];
            }
            return $return_data;

            $attribute = AttributeService::getInstance()->getList(1, count($attribute_value_arr), 'desc', 'id',
                ['groupOp' => 'AND', 'rules' => [
                    ['field' => 'attribute_value', 'op' => 'in', 'data' => '"' . implode('", "', $attribute_value_arr) . '"']
                ]]);
            var_dump($attribute);
            var_dump($attribute_value_arr);
            exit();
        } catch (ModelDriverException $e) {
            throw $e;
        } catch (ModelException $e) {
            throw $e;
        } catch (ModelReflectionException $e) {
            throw $e;
        } catch (ModelSqlException $e) {
            throw $e;
        } catch (ServiceException $e) {
            throw $e;
        } catch (TransformerException $e) {
            throw $e;
        }
    }

}
